///////////////////////////////////////////////////////////////////////
// File:	MessageSystem.cpp
//
// Author: Roberto Rubiano
// 
// Purpose: function definitions
///////////////////////////////////////////////////////////////////////
#include "MessageSystem.h"

void MessageSystem::InitMessageSystem(MESSAGEPROC pMsgProc)
{
	if(!pMsgProc)	// Error Check
		return;

	m_MSGPROC = pMsgProc;
}

//	Sends the message into the queue and awaits processing later on through
//	The ProcessMessages() function.
void MessageSystem::SendMsg(CBaseMessage* pMsg)
{
	if(!pMsg)	// Error Check
		return;

	m_MSGQ.push(pMsg);
}

//	Processes all the messages that are waiting inside of the queue.
//	Normally you only call this function once per frame.
void MessageSystem::ProcessMessages(void)
{
	if(!m_MSGPROC) // Error Check
		return;

	while(!m_MSGQ.empty())
	{
		m_MSGPROC(m_MSGQ.front());			//	Process the first message.
		SAFE_DELETE(m_MSGQ.front());		//	Delete the message memory.
		m_MSGQ.pop();						//	Go to the next message.
	}
}

//	Clears any messages that may be remaining
void MessageSystem::ClearMessages(void)
{
	while(!m_MSGQ.empty())
	{
		SAFE_DELETE(m_MSGQ.front());		//	Delete the message memory.
		m_MSGQ.pop();						//	Go to the next message.
	}
}

//	Clears any messages that may be remaining and sets the function pointer to NULL.
void MessageSystem::ShutdownMessageSystem(void)
{
	ClearMessages();

	m_MSGPROC = NULL;
}